layui.define(function (exports) {
    "use strict";

    var getBetween = function (str) {
        const regex = /{(.*?)}/;
        const match = str.match(regex);
        if (match) {
            const result = match[1]; // 获取匹配到的内容
            return result;
        } else {
            return false;
        }
    }

    /**
     * html转义
     * @param {string} html 
     * @returns 
     */
    var escape = function (html) {
        var div = document.createElement('div');
        div.appendChild(document.createTextNode(html));
        return div.innerHTML;
    }

    var jmTpl = function (template, data) {

        // 模板必须为 string 类型
        if (typeof template !== 'string') return template;
        template = template
            .replace(/\{\s*\n*\s*/g, "{")
            .replace(/\s*\n*\s*\}/g, "}");

        var templateArr = template.split("\n");
        var strTem = "var view = '';";
        strTem +=
            `var keys = Object.keys(app);
        for (var i = 0; i < keys.length; i++) {
            var key = keys[i];
            window[key] = app[key];
        }`
        for (var i = 0; i < templateArr.length; i++) {
            if (templateArr[i].trim() === "") {
                continue
            } else {
                var gb = getBetween(templateArr[i].trim());
                if (gb !== false) {
                    if (gb === "/if") {
                        strTem += "}"
                    } else if (gb === ":else") {
                        strTem += "}else{"
                    } else if (gb.includes(":else if")) {
                        strTem += `}else if(${gb.replace(`:else if`, ``)}){`
                    } else if (gb.includes("#if")) {
                        strTem += `if(${gb.replace(`#if`, ``)}){`
                    } else if (gb === "/each") {
                        strTem += `});`
                    } else if (gb.includes("#each")) {
                        var arr = gb.replace(`#each`, ``).split(" ")
                        strTem += `${arr[1]}.forEach(function(${arr[3]}){`
                    } else if (gb.includes("@html")) {
                        templateArr[i] = templateArr[i].replace(/(\{\@html\s([^}]+)\})/g, function (m, p1, p2) {
                            return `\${${p2}}`
                        })
                        strTem += `view += \`${templateArr[i]}\`;\n`
                    } else {
                        templateArr[i] = templateArr[i].trim().replace(/(\{([^}]+)\})/g, function (m, p1, p2) {
                            return `\${escape(${p2})}`
                        })
                        strTem += `view += \`${templateArr[i]}\`;\n`
                    }
                } else {
                    strTem += `view += \`${templateArr[i].trim()}\`;\n`
                }
            }
        }

        try {
            var fn = new Function(`app,escape`, `${strTem}return view;`)
            return typeof fn(data, escape) !== "undefined" ? fn(data, escape) : "";
        } catch (e) {
            console.error(`${e}\n${strTem}`)
            return strTem;
        }
    }

    exports('jmTpl', jmTpl); // 输出模块
});  